#include "StdAfx.h"
#include "PrintBigInt.h"


PrintBigInt::PrintBigInt(void)
{
}


PrintBigInt::~PrintBigInt(void)
{
}

void PrintBigInt::Print1ToN(int n){
	if(n <= 0){
		return ;
	}
	int index = n-1;
	char* bigIntChar = new char[n+1];
	for(int i = 0;i<n;i++){
		bigIntChar[i] = '0';
		
	}
	bigIntChar[n]='\0';
	while(!NumberOverflow(bigIntChar,index));

	delete []bigIntChar;
}

bool PrintBigInt::NumberOverflow(char* bigInt,int index){
	bool overflow = false;
	bigInt[index] += 1;
	//cout<<bigInt[index] <<"\t";
	bool carry = false;
	for(int i = index;i>=0;i--){
		if(carry){
			bigInt[i] += 1;
			carry = false;
		}
		if(bigInt[i] >= ('0'+10)){
			if(i==0){
				overflow=true;
				break;
			}
			carry = true;
			bigInt[i] ='0';
		}else{
			break;
		}
	}
	cout<<bigInt<<'\t';
	return overflow;
}

void PrintBigInt::Print1ToNRecursion(int n){
	if(n <= 0){
		return ;
	}
	int index = n-1;
	char* bigIntChar = new char[n+1];
	for(int i = 0;i<n;i++){
		bigIntChar[i] = '0';
		
	}
	bigIntChar[n]='\0';

	RecursionPrint(bigIntChar,n,index);
}

void PrintBigInt::RecursionPrint(char* bigInt,int length,int index){
	
	for(int i = 0;i<10;i++){
		
		if(index!=0){
			RecursionPrint(bigInt,length,index-1);
		}
		bigInt[index] += 1;
		
		if(bigInt[index] >= '9'){
			cout<<bigInt<<'\t';
			bigInt[index] = '0';
		}else{
			cout<<bigInt<<'\t';
			
		}
	}
	
}

void PrintBigInt::Div(const int x,const int y){
	
	int multi = 0;
	int result = 0;
	int dividend = x;
	while(dividend>=y){
		multi = 1;
		while(y*multi<=(dividend)){
			multi = multi<<1;
		}
		multi = multi>>1;
		result = result+multi;
		dividend = dividend-multi*y;
	}
	cout<<result<<endl;
}

int PrintBigInt::NumberOf1(int n){
	int times = 0;
	while(n != 0){
		n = n&(n-1);
		times++;
	}
	return times;
}